<?php

/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: YouhuiAction.class.php 2011-10-31 11:55:16 wufujun $
 */

if (!defined('THINK_PATH')) {
	exit('Access Denied');
}

class YouhuiAction extends BpAction{
	function _filter(&$map){
		$map['title'] = array('like',"%".$_POST['title']."%");
	}
	
	//赋值可用分类
	public function _before_add(){
		$YouhuiClassModel = D('YouhuiClass');
		$YouhuiClass = $YouhuiClassModel->where('status = 1')->select();
		$this->assign('YouhuiClass',$YouhuiClass);
	}
	
	//赋值可用分类
	public function _before_edit(){
		$YouhuiClassModel = D('YouhuiClass');
		$YouhuiClass = $YouhuiClassModel->where('status = 1')->select();
		$this->assign('YouhuiClass',$YouhuiClass);
	}
	
	//通过siteid获取website_name
	public function getWebsiteName(){
		$siteid  =  $_REQUEST['siteid'];
		$sitename = getWebsiteName($siteid);
		if($sitename){
			$this->success('所属网站：'.$sitename);
		}else{
			$this->error('没有找到网站');
		}
	}
	
	public function insert(){
		$model = D ('Youhui');
		$attModel = 'youhui';
		//日期处理
		$_POST['stime'] = strtotime($_POST['stime']);
		$_POST['etime'] = strtotime($_POST['etime']);
		//购买链接处理
		$_POST['link'] = urlencode($_POST['link']);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		if(!empty($_FILES['img']['name'])){
			$image =  $this->_upload('youhui');
			$model->img = $attModel."/".$image;
		}
		
		$list = $model->add ();
		if ($list !== false) {
			$this->success('添加成功！');
		}else{
			$this->error('新增失败！');
		}
	}
	
	public function update(){
		$model = D ('Youhui');
		//所属附件模型，主要用于上传附件时生成相关目录
		$attModel = 'youhui';
		//日期处理
		$_POST['stime'] = strtotime($_POST['stime']);
		$_POST['etime'] = strtotime($_POST['etime']);
		//购买链接处理
		$_POST['link'] = urlencode($_POST['link']);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		if(!empty($_FILES['img']['name'])){
			$image =  $this->_upload('youhui');
			$model->img = $attModel."/".$image;
		}
		
		$list = $model->save ();
		if ($list !== false) {
			$this->success('编辑成功！');
		}else{
			$this->error('编辑失败！');
		}
	}


	//删除文章时删除图片
	public function _before_foreverdelete() {
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		if(isset($id)){
			$src =APP_USER.'/attachment/'.D('Youhui')->where($model->getPk()."=".$id)->getField('img');
			$thumb_m = $src.'.m.jpg';
			$thumb_s = $src.'.s.jpg';
			if(is_file($src)){
				unlink($src);
			}
			if(is_file($thumb_m)){
				unlink($thumb_m);
			}
			if(is_file($thumb_s)){
				unlink($thumb_s);
			}
		}
	}
	
	//删除图片
	public function delimg(){
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		if(isset($id)){
			$src =APP_USER.'/attachment/'.D('Youhui')->where($model->getPk()."=".$id)->getField('img');
			$thumb_m = $src.'.m.jpg';
			$thumb_s = $src.'.s.jpg';
			if(is_file($src)){
				unlink($src);
			}
			if(is_file($thumb_m)){
				unlink($thumb_m);
			}
			if(is_file($thumb_s)){
				unlink($thumb_s);
			}
		}
		D($name)->where($model->getPk()."=".$id)->setField('img','');
		
		$this->success('图片删除成功');
	}

	//处理过期优惠信息
	public function clearexpired(){
		$name = $this->getActionName();
		$model = M($name);
		$nowTime = time();
		$data['status'] = 0;
		$model->where("etime < $nowTime and status = 1")->data($data)->save();
		$this->success('清理成功');
	}
}
?>
